package com.zhiwang.xiaoqu.ui.withdraw

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.ktextension.setImmersive
import com.zhiwang.xiaoqu.ui.BaseActivity
import com.zhiwang.xiaoqu.ui.withdraw.WithDrawActivity.*
import com.zhiwang.xiaoqu.util.ImeUtil
import com.zhiwang.xiaoqu.widget.WithDrawSelector
import kotlinx.android.synthetic.main.activity_with_draw.*

class WithDrawActivity : BaseActivity(), WithDrawFragmentInteractListener {

    val fragments = arrayListOf<Fragment>()

    val balance = 120

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setImmersive(Color.TRANSPARENT, true)
        setContentView(R.layout.activity_with_draw)

        if (savedInstanceState != null) {
            val aliFragment =
                supportFragmentManager.findFragmentByTag(AliWithdrawFragment::class.java.name) ?: AliWithdrawFragment()

            val wxFragment =
                supportFragmentManager.findFragmentByTag(WeiXinFragment::class.java.name) ?: WeiXinFragment()

            fragments.add(aliFragment)
            fragments.add(wxFragment)
        } else {
            fragments.add(AliWithdrawFragment())
            fragments.add(WeiXinFragment())
        }

        val transaction = supportFragmentManager.beginTransaction()
        for (f in fragments) {
            if (f.isAdded.not())
                transaction.add(R.id.withdraw_container, f, f::class.java.name)
            else transaction.hide(f)
        }
        transaction.commit()

        withdraw_selector.onSelectChangeListener = object : WithDrawSelector.OnSelectChangeListener {
            override fun onSelectChange(index: Int) {
                ImeUtil.hideIme(this@WithDrawActivity)
                supportFragmentManager.beginTransaction()
                    .hide(fragments[1 - index])
                    .show(fragments[index])
                    .commit()
            }

        }
    }

    override fun checkBalanceWithDrawEnabled(num: Int): Boolean {
        return num <= balance
    }


}

interface WithDrawFragmentInteractListener {

    fun checkBalanceWithDrawEnabled(num: Int): Boolean
}